package com.SouthernWall_404.AgeRight.Common.Event;

import com.SouthernWall_404.AgeRight.Common.API.LockAPI.LockableBlocksRegistry;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;

public class LockableBlocksEventHandler {
    public static void setup() {
        IEventBus modEventBus = FMLJavaModLoadingContext.get().getModEventBus();

        // 在 CommonSetup 阶段触发注册事件，确保所有方块已注册
        modEventBus.addListener(LockableBlocksEventHandler::onCommonSetup);
    }

    private static void onCommonSetup(final FMLCommonSetupEvent event) {
        // 在事件队列中延迟执行，确保所有模组已初始化
        event.enqueueWork(() -> {
            // 创建并发布注册事件
            RegisterLockableBlocksEvent registerEvent = new RegisterLockableBlocksEvent(
                    LockableBlocksRegistry::registerBlock,
                    LockableBlocksRegistry::registerBlockTag,
                    LockableBlocksRegistry::registerCondition
            );

            // 发布到 MOD 事件总线
            FMLJavaModLoadingContext.get().getModEventBus().post(registerEvent);
        });
    }
}